<?php
/**
 * Created by PhpStorm.
 * User: Bzing
 * Date: 2017/7/13
 * Time: 17:17
 */
namespace app\api\controller;
use app\models\Models;

class Dynamic extends Base {
    /**
     * 动态列表
     * @return \think\response\Json
     */
    public function dynamicList(){
        if(request()->isPost()){
            $params = input('param.');
            try{
                // 验证签名
                self::sign(['equipment','user_id','page']);
                $dynamic = new Models('dynamic');
                $field = 'a.id,a.descrip,a.image,a.create_time,b.nickname,b.sex,b.birth,b.portrait';
                $list['list'] = $dynamic->_page(['a.uid'=>$params['user_id'],'a.status'=>Models::STATUS_ON],$field,'a.id desc',[['user b','a.uid = b.id','left']],$params['page']);
                $list['list'] = $list['list']->items();
                foreach($list['list'] as $k=>$v){
                    $list['list'][$k]['create_time'] = formatDate($v['create_time']);
                    $list['list'][$k]['portrait']    = empty($v['portrait']) ? '' : self::arrayMapPortrait($v['portrait']);
                    $list['list'][$k]['image']       = empty($v['image']) ? '' : array_map([$this,'arrayMapDynamic'],json_decode($v['image'],true));
                    $list['list'][$k]['age']         = calcAge($v['birth']);
                    unset($list[$k]['birth']);
                }
                // 统计条数
                $list['count'] = count($list['list']);
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($list));
        }
    }

    /**
     * 添加动态
     * @return \think\response\Json
     */
    public function dynamicAdd(){
        if(request()->isPost()){
            try{
                // 验证签名
                $params = self::checkLogin(['equipment','encrypt']);
                $dynamic = new Models('dynamic');
                if($_FILES){
                    $add['image'] = json_encode(upload('dynamic'));
                }
                $add['uid']     = $params['uid'];
                $add['descrip'] = $params['descrip'];
                $add['create_time'] = time();
                $bool = $dynamic->_add($add);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult());
        }
    }

    /**
     * 删除动态
     * @return \think\response\Json
     */
    public function dynamicDel(){
        if(request()->isPost()){
            try{
                // 验证签名
                $params = self::checkLogin(['equipment','encrypt','dynamic_id']);
                $dynamic = new Models('dynamic');
                $info = $dynamic->_find($params['dynamic_id']);
                if($info['status'] != Models::STATUS_ON){
                    throw new \LogicException('该动态不存在或已删除',1038);
                }
                $bool = $dynamic->_save(['id'=>$params['dynamic_id'],'status'=>Models::STATUS_DELETE]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult());
        }
    }
}